package com.jxt.myjxtmanager.ui.model;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.jxt.myjxtmanager.db.Student;
import com.jxt.myjxtmanager.db.StudentModel;
import com.jxt.myjxtmanager.entity.CheckWorkListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qm on 2017/2/10 0010.
 */
public class CheckWorkMo {

    /**
     * 总数据
     */
    private List<Student> queryList;
    private final Context context;
    private OnFindAllDataListener onFindAllDevicesListener;

    private String lateNum;
//    private String str = "{\n" +
//            "    \"code\": 1,\n" +
//            "    \"error_code\": 0,\n" +
//            "    \"is_msg\": 0,\n" +
//            "    \"late_num\": 5,\n" +
//            "    \"result\": [\n" +
//            "        {\n" +
//            "            \"studentNum\": \"LQ100022305\",\n" +
//            "            \"studentName\": 张三丰,\n" +
//            "            \"intoSchoolTimeAM\": 7：35,\n" +
//            "            \"outSchoolTimeAM\": 12：05,\n" +
//            "            \"intoSchoolTimePM\": 1：40,\n" +
//            "            \"outSchoolTimePM\": \"5：35\",\n" +
//            "            \"todayCheckWork\": 全勤,\n" +
//            "            \"isLate\": \"\"\n" +
//            "        },\n" +
//            "        {\n" +
//            "            \"studentNum\": \"LQ100022305\",\n" +
//            "            \"studentName\": 张三丰,\n" +
//            "            \"intoSchoolTimeAM\": 7：35,\n" +
//            "            \"outSchoolTimeAM\": 12：05,\n" +
//            "            \"intoSchoolTimePM\": 1：40,\n" +
//            "            \"outSchoolTimePM\": \"5：35\",\n" +
//            "            \"todayCheckWork\": 全勤,\n" +
//            "            \"isLate\": \"\"\n" +
//            "        },\n" +
//            "        {\n" +
//            "            \"studentNum\": \"LQ100022305\",\n" +
//            "            \"studentName\": 张三丰,\n" +
//            "            \"intoSchoolTimeAM\": 7：35,\n" +
//            "            \"outSchoolTimeAM\": 12：05,\n" +
//            "            \"intoSchoolTimePM\": 1：40,\n" +
//            "            \"outSchoolTimePM\": \"5：35\",\n" +
//            "            \"todayCheckWork\": 全勤,\n" +
//            "            \"isLate\": \"\"\n" +
//            "        },\n" +
//            "        {\n" +
//            "            \"studentNum\": \"LQ100022305\",\n" +
//            "            \"studentName\": 张三丰,\n" +
//            "            \"intoSchoolTimeAM\": 7：35,\n" +
//            "            \"outSchoolTimeAM\": 12：05,\n" +
//            "            \"intoSchoolTimePM\": 1：40,\n" +
//            "            \"outSchoolTimePM\": \"5：35\",\n" +
//            "            \"todayCheckWork\": 全勤,\n" +
//            "            \"isLate\": \"\"\n" +
//            "        }\n" +
//            "    ],\n" +
//            "    \"msg\": null,\n" +
//            "    \"success\": true,\n" +
//            "    \"failed\": false,\n" +
//            "    \"error\": false\n" +
//            "}";

    private String str = "{\n" +
            "    \"code\": 1,\n" +
            "    \"error_code\": 0,\n" +
            "    \"is_msg\": 0,\n" +
            "    \"late_num\": 5,\n" +
            "    \"result\": [\n" +
            "        {\n" +
            "            \"id\": 100000001311,\n" +
            "            \"studentNum\": \"LQ100022304\",\n" +
            "            \"studentName\": 马原,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2005-04,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18656897531\",\n" +
            "            \"urgentPhone\": \"15836599563\",\n" +
            "            \"intoSchoolTimeAM\": \"7：35\",\n" +
            "            \"outSchoolTimeAM\": \"12：05\",\n" +
            "            \"intoSchoolTimePM\": \"1：40\",\n" +
            "            \"outSchoolTimePM\": \"5：35\",\n" +
            "            \"todayCheckWork\": \"全勤\",\n" +
            "            \"isLate\": \"未迟到\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001312,\n" +
            "            \"studentNum\": \"LQ100022305\",\n" +
            "            \"studentName\": 刘伟,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2005-02,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"13756897531\",\n" +
            "            \"urgentPhone\": \"15836599456\",\n" +
            "            \"intoSchoolTimeAM\": \"7：35\",\n" +
            "            \"outSchoolTimeAM\": \"12：05\",\n" +
            "            \"intoSchoolTimePM\": \"1：40\",\n" +
            "            \"outSchoolTimePM\": \"5：35\",\n" +
            "            \"todayCheckWork\": \"全勤\",\n" +
            "            \"isLate\": \"未迟到\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001313,\n" +
            "            \"studentNum\": \"LQ100022306\",\n" +
            "            \"studentName\": 阿迪斯,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2005-10,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18656897895\",\n" +
            "            \"urgentPhone\": \"15836595648\",\n" +
            "            \"intoSchoolTimeAM\": \"7：35\",\n" +
            "            \"outSchoolTimeAM\": \"12：05\",\n" +
            "            \"intoSchoolTimePM\": \"1：40\",\n" +
            "            \"outSchoolTimePM\": \"5：35\",\n" +
            "            \"todayCheckWork\": \"全勤\",\n" +
            "            \"isLate\": \"未迟到\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001314,\n" +
            "            \"studentNum\": \"LQ100022307\",\n" +
            "            \"studentName\": 逗你玩,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2004-05,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18655867531\",\n" +
            "            \"urgentPhone\": \"15836599023\",\n" +
            "            \"intoSchoolTimeAM\": \"7：35\",\n" +
            "            \"outSchoolTimeAM\": \"12：05\",\n" +
            "            \"intoSchoolTimePM\": \"1：40\",\n" +
            "            \"outSchoolTimePM\": \"5：35\",\n" +
            "            \"todayCheckWork\": \"全勤\",\n" +
            "            \"isLate\": \"未迟到\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001351,\n" +
            "            \"studentNum\": \"LQ100022308\",\n" +
            "            \"studentName\": 小明,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2006-02,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18656897452\",\n" +
            "            \"urgentPhone\": \"15936599036\",\n" +
            "            \"intoSchoolTimeAM\": \"7：35\",\n" +
            "            \"outSchoolTimeAM\": \"12：05\",\n" +
            "            \"intoSchoolTimePM\": \"1：40\",\n" +
            "            \"outSchoolTimePM\": \"5：35\",\n" +
            "            \"todayCheckWork\": \"全勤\",\n" +
            "            \"isLate\": \"未迟到\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001316,\n" +
            "            \"studentNum\": \"LQ100022309\",\n" +
            "            \"studentName\": 小明他哥,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2006-04,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"18756845862\",\n" +
            "            \"urgentPhone\": \"13936125896\",\n" +
            "            \"intoSchoolTimeAM\": \"7：55\",\n" +
            "            \"outSchoolTimeAM\": \"12：05\",\n" +
            "            \"intoSchoolTimePM\": \"1：40\",\n" +
            "            \"outSchoolTimePM\": \"5：35\",\n" +
            "            \"todayCheckWork\": \"全勤\",\n" +
            "            \"isLate\": \"迟到\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 100000001317,\n" +
            "            \"studentNum\": \"LQ100022310\",\n" +
            "            \"studentName\": 大明,\n" +
            "            \"sex\": 男,\n" +
            "            \"birthday\": 2005-07,\n" +
            "            \"address\": 1,\n" +
            "            \"myClass\": \"三年级六班\",\n" +
            "            \"parentName\": \"赵梅\",\n" +
            "            \"parentPhone\": \"15346985203\",\n" +
            "            \"urgentPhone\": \"18246598205\",\n" +
            "            \"intoSchoolTimeAM\": \"7：35\",\n" +
            "            \"outSchoolTimeAM\": \"12：05\",\n" +
            "            \"intoSchoolTimePM\": \"1：40\",\n" +
            "            \"outSchoolTimePM\": \"5：35\",\n" +
            "            \"todayCheckWork\": \"请假一次\",\n" +
            "            \"isLate\": \"未迟到\"\n" +
            "        }\n" +
            "        \n" +
            "    ],\n" +
            "    \"msg\": null,\n" +
            "    \"success\": true,\n" +
            "    \"failed\": false,\n" +
            "    \"error\": false\n" +
            "}";

    public CheckWorkMo(Context context) {
        this.context = context;
        queryList = new ArrayList<>();
        mGson = new Gson();
    }

    public List<Student> getQueryList() {
        return queryList;
    }

    public String getLateNum() {
        return lateNum;
    }

    public void RequestData() {

//        HttpClient.getInstance().httpCheckWorkInfo(context, new ResultCallback() {
//            @Override
//            public void onFailure(Request request, Exception e) {
//                super.onFailure(request, e);
//            }
//
//            @Override
//            public void onError(int code, String message) {
//                super.onError(code, message);
//            }
//
//            @Override
//            public void onResponse(Object response) {
//                super.onResponse(response);
//            }
//
//            @Override
//            public void onResponse(int code, String msg) {
//                super.onResponse(code, msg);
//            }
//
//            @Override
//            public void onFinish() {
//                super.onFinish();
//            }
//
//            @Override
//            public void onStart() {
//                super.onStart();
//            }
//        });

        loadData();
    }

    private Gson mGson;

    private void loadData() {
        //模拟网络加载
        Message message = new Message();
        message.arg1 = 2;
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    private Handler loadDataHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            queryList = StudentModel.getInstance().lazyLoadmessage();
            if (queryList != null && queryList.size() > 0) {
            } else {
                CheckWorkListBean mResult = mGson.fromJson(str, CheckWorkListBean.class);
                queryList = mResult.getWorkBeans();
                lateNum = mResult.getLate_num();
                StudentModel.getInstance().insertOrReplaceAll(queryList);
            }
            onFindAllDevicesListener.onFindSuccess(queryList);
        }
    };


    public void setOnFindAllDevicesListener(OnFindAllDataListener onFindAllDevicesListener) {
        this.onFindAllDevicesListener = onFindAllDevicesListener;
    }

    /**
     * 请求监听
     */
    public interface OnFindAllDataListener {
        void onFindSuccess(List<Student> workBeans);

        void OnFindAllDataSuccess(String strTitle);

        void onFailure();

        void onError();
    }
}
